#include "stdafx.h"

#include "LaborTextureManager.h"

#include "../Common/LaborFileSystem.h"
#include "../LaborCore.h"
#include "../Common/LaborLogger.h"
NAMESPACE_LABOR3D_BEGIN

xTextureName::xTextureName()
{
	m_Name       = L"";
	//m_InitDesc.m_TextureDesc.m_fmt        = PIXELFORMAT_None;
	//m_Type       = RESOURCE_TEXTURE;
}

xTextureName::xTextureName(const wchar_t* fileName)
{
	m_Name       = fileName;
	//m_InitDesc.m_TextureDesc.m_fmt        = PIXELFORMAT_None;
	//m_Type       = RESOURCE_TEXTURE;
}

//xTextureName::xTextureName(const wchar_t* _Name , ePIXEL_FORMAT fmt , eResourceType type ,bool rtt, bool bLockable , int w , int h , int depth , int arraySize)
//{
//	m_Name       = _Name;
//	m_InitDesc.m_TextureDesc.m_width      = w ;
//	m_InitDesc.m_TextureDesc.m_height     = h ;
//	m_InitDesc.m_TextureDesc.m_depth      = depth ;
//	m_InitDesc.m_TextureDesc.m_fmt        = fmt;
//    m_InitDesc.m_bReadable = bLockable;
//    if(type == RESOURCE_TEXTURECube)
//    {
//        m_InitDesc.m_TextureDesc.m_nArraySize = eTextureArray_CubeMap;
//    }
//	m_Type       = type;
//
//}

LaborBaseTextureMgr*  xBaseTextureLoader::createInstance( ILRenderSystem* pRenderSys, const wchar_t* _name)
{
    LaborBaseTextureMgr* pMgr = new LaborBaseTextureMgr(_name);
    pMgr->setThis(pMgr);
    pMgr->setRenderSys(pRenderSys);
    return pMgr;
}

xBaseTextureLoader::xBaseTextureLoader()
{
    m_pRenderSys = NULL;
}

bool  xBaseTextureLoader::_isResLoaded(ILaborTexture* pRes)
{
    return pRes && pRes->isLoaded();
}

HBaseTexture xBaseTextureLoader::load(const xTextureName& texName , bool bLoadImm  , bool arg)
{
     LaborBaseTextureMgr* pMgr = m_pThis;
     return pMgr->add(texName, arg ,bLoadImm);
}

HBaseTexture  xBaseTextureLoader::loadTexture(const wchar_t* texName, bool bLoadImm  , bool arg)
{
	xTextureName _name(texName);
	return load(_name ,bLoadImm , arg);
}

//HBaseTexture  xBaseTextureLoader::loadTexture(const wchar_t* texName , xTextureInitDesc& initDesc , eResourceType type) 
//{
//     xTextureName _name(texName);
//	 _name.m_InitDesc = initDesc;
//	 _name.m_Type = type;
//	 return load(_name,true , 0);
//}

ILaborTexture*   xBaseTextureLoader::_loadTexture(const  xTextureName& _texName , const wchar_t* fileName ,unsigned char* buf , int bufLen , unsigned int arg)
{
	const wchar_t* _extName = xFileSystem::singleton()->getFileExtName(_texName.m_Name.c_str() );
	ITextureLoader* pTextureLoader = xTextureLdrMgr::singleton()->findTextureLoader(_extName);
	if(pTextureLoader == NULL)
		return GetLaborCore()->getRenderSystem()->createTextureFromFile( fileName );
		//return m_pRenderApi->createFileTexture( fileName , buf,bufLen,arg , &_texName.m_InitDesc);
	return pTextureLoader->loadTexture( fileName , buf , bufLen , _texName);

}

bool  xBaseTextureLoader::_loadTexture(const xTextureName& _texName, ILaborTexture*& pTexture , int& ResSize , unsigned int arg)
{
	const wchar_t* texName = _texName.m_Name.c_str(); 
	std::ds_wstring full_name;
	ResSize = 0;

	if(LABOR_IsAbsPath( texName  ) )
	{
		pTexture = _loadTexture(_texName , texName,NULL,0,arg);
		if( pTexture != NULL)
		{
			ResSize = pTexture->memUsage();
			LABOR_LOG(eLLL_DEBUG_NORMAL,L"Load from abs path name = %s \n", texName );
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		int nResItem = (int)this->m_Packages.size();
		for(int i = 0 ; i < nResItem ; i++)
		{
			xResPkgPathItem& item = this->m_Packages[i];

			full_name = item.m_ResDir + ds_wstring(texName)  ;
			pTexture = _loadTexture(_texName , full_name.c_str() , NULL , 0  , arg);
			if( pTexture != NULL )
			{
				ResSize = pTexture->memUsage();
				LABOR_LOG(eLLL_DEBUG_TIPS,L"Load from path, search index = %d , name = %s \n",i,texName );
				return true;
			}
			else
			{
				;
			}

		}
	}

	return false;
}

bool  xBaseTextureLoader::_loadResource(const xTextureName& strResName   , ILaborTexture* & pRes , int& ResSize, unsigned int arg)
{
 //   bool bReadable = (strResName.m_InitDesc.m_access & RESOURCE_ACCESS_READ) != 0 ;
 //   const xTextureDesc& textDesc = strResName.m_InitDesc.m_TextureDesc;

	//if(strResName.m_Type == RESOURCE_TEXTURE)
	//{
		return _loadTexture(strResName.m_Name.c_str() , pRes , ResSize , arg);        
	//}
	//else if(strResName.m_Type == RESOURCE_TEXTURE2D)
	//{
 //       if(strResName.m_InitDesc.m_bindType & BIND_AS_RENDER_TARGET)
 //           pRes = m_pRenderApi->createRenderableTexture(textDesc.m_width , textDesc.m_height , textDesc.m_depth , textDesc.m_fmt , bReadable); 
	//	else
 //           pRes = m_pRenderApi->createTexture(strResName.m_InitDesc , NULL , 0);
	//	   
	//	return pRes != NULL;
	//}
	//else if(strResName.m_Type == RESOURCE_RENDERTARGET)
	//{
	//	IRenderTarget* pRT = m_pRenderApi->createRenderTarget(textDesc.m_width , textDesc.m_height ,  textDesc.m_fmt , false , true);
	//	pRes = pRT->toTexture();
	//	return pRes != NULL;
	//}
	//else if(strResName.m_Type == RESOURCE_DEPTHBUFFER)
	//{
	//	IRenderTarget* pRT = m_pRenderApi->createDepthBuffer(textDesc.m_width , textDesc.m_height ,  textDesc.m_fmt , false , false);
	//	pRes = pRT->toTexture();
	//	return pRes != NULL;
	//}
	return false;
}

bool  xBaseTextureLoader::_unloadResource(const xTextureName& strResName , ILaborTexture* & pRes , unsigned int& TotalResSize)
{
	int ResSize = TotalResSize - pRes->memUsage();
	if(ResSize < 0) ResSize = 0;
	TotalResSize = ResSize;
	pRes->unload();
	return false;
}

void  xBaseTextureLoader::_deleteResource(const xTextureName& strResName , ILaborTexture* pRes)
{
	pRes->KillObject();
	pRes=NULL;
	return ;
}

unsigned int  xBaseTextureLoader::_getResSize(ILaborTexture* pRes)
{
	if(pRes == NULL)
		return 0;
	return pRes->memUsage();
}

NAMESPACE_LABOR3D_END

